package com.qdxwx.manage.controller.channel;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.channel.ChannelService;
import com.qdxwx.manage.annotation.Log;
import com.qdxwx.common.annotation.Permissions;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.models.channel.Channel;
import com.qdxwx.models.enums.BusinessType;
import com.qdxwx.models.enums.ModuleType;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletRequest;

/**
 * 频道设置Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/channel")
public class ChannelEditController extends BaseController {
    @Autowired
    private ChannelService channelService;
    /**
     * 新增
     */
    @GetMapping("/add.html")
    public String add(HttpServletRequest request, ModelMap mmap) {
        mmap.put("modules", ModuleType.values());
        Channel channel = new Channel();
        channel.setChannelId(0);
        channel.setChannelType(1);
        channel.setIsBlank(0);
        mmap.put("modules", ModuleType.values());
        mmap.put("channel", channel);
        return "channel/channel-edit";
    }

    /**
     * 修改
     */
    @GetMapping("/edit.html")
    public String edit(HttpServletRequest request, ModelMap mmap) {
        int channelId = TKConvert.toInteger(request.getParameter("id"));
        Channel channel = channelService.get(channelId);
        if (channel == null) return error404;
        mmap.put("modules", ModuleType.values());
        mmap.put("channel", channel);
        return "channel/channel-edit";
    }

    /**
     * 修改操作
     */
    @PostMapping("/save.json")
    @Permissions("channel:edit")
    @ResponseBody
    public AjaxResult edit(HttpServletRequest request) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        String channelName = request.getParameter("channelName");
        if (TKString.isEmpty(channelName)) return error("请输入频道名称");
        String linkUrl = request.getParameter("linkUrl");
        if (TKString.isEmpty(linkUrl)) return error("请输入频道链接地址");
        String intro = request.getParameter("intro");
        if (TKString.isEmpty(intro)) return error("请输入频道说明");

        String itemName = request.getParameter("itemName");
        String itemUnit = request.getParameter("itemUnit");
        int sort = TKConvert.toInteger(request.getParameter("sort"));
        Channel channel;
        if (channelId > 1) {
            channel = channelService.get(channelId);
            if (channel == null) {
                return error("找不到指定参数");
            }
        } else {
            int channelType = TKConvert.toInteger(request.getParameter("channelType"));
            int moduleId = TKConvert.toInteger(request.getParameter("moduleId"));
            String channelDir = request.getParameter("channelDir");
            channel = new Channel();
            channel.setChannelType(channelType);
            channel.setModuleId(moduleId);
            channel.setChannelDir(channelDir);
        }
        if (channel.getChannelType() < 2) {
            if (channel.getModuleId() < 1) return error("请选择使用模块");
            if (TKString.isEmpty(channel.getChannelDir())) return error("请输入频道目录");
            if (TKString.isEmpty(itemName)) return error("请输入项目名称");
            if (TKString.isEmpty(itemUnit)) return error("请输入项目单位");
        }
        channel.setChannelName(channelName);
        channel.setLinkUrl(linkUrl);
        channel.setIntro(intro);
        channel.setIsHide(TKConvert.toInteger(request.getParameter("isHide")) >= 1 ? 1 : 0);
        channel.setIsBlank(TKConvert.toInteger(request.getParameter("isBlank")) >= 1 ? 1 : 0);
        channel.setIsDisabled(TKConvert.toInteger(request.getParameter("isDisabled")) >= 1 ? 1 : 0);
        channel.setItemName(itemName);
        channel.setItemUnit(itemUnit);
        channel.setSort(sort);
        if (channelId > 0) {
            return toAjax(channelService.update(channel));
        } else {
            return toAjax(channelService.insert(channel));
        }
    }

    /**
     * 删除
     */
    @Log(title = "删除频道", businessType = BusinessType.DELETE)
    @Permissions("channel:remove")
    @PostMapping("/remove.json")
    @ResponseBody
    public AjaxResult remove(HttpServletRequest request) {
        int id = TKConvert.toInteger(request.getParameter("ids"));
        if (id < 1) return error("参数异常");
        Channel channel = channelService.get(id);
        if (channel == null) return error("找不到指定数据");
        if (channel.getChannelType().equals(0)){
            return error("系统频道不得删除");
        }
        return toAjax(channelService.delete(id));
    }

    /**
     * 禁用或启用
     */
    @PostMapping("/disabled.json")
    @Permissions("channel:edit")
    @ResponseBody
    public AjaxResult disabled(HttpServletRequest request) {
        int channelId = TKConvert.toInteger(request.getParameter("channelId"));
        if (channelId < 1) return error("参数异常");
        int isDisabled = TKConvert.toInteger(request.getParameter("isDisabled"));
        Channel channel = new Channel();
        channel.setChannelId(channelId);
        channel.setIsDisabled(isDisabled >= 1 ? 1 : 0);
        return toAjax(channelService.updateDisabled(channel));
    }

}
